/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.services.applications;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.zoundry.gaedt.core.models.application.ZGaeApplicationModel;


/**
 * A resource visitor that looks for Google App Engine applications.  This
 * visitor essentially looks for app.yaml files.
 */
public class ZGaeApplicationFindingResourceVisitor implements IResourceVisitor
{
    /** The set of applications found by the visitor. */
    private Set<IZGaeApplication> mApplications = new HashSet<IZGaeApplication>();

    /**
     * Default c'tor.
     */
    public ZGaeApplicationFindingResourceVisitor()
    {
    }

    /**
     * @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource)
     */
    public boolean visit(IResource aResource) throws CoreException
    {
        if (aResource.getType() == IResource.FILE)
        {
            if (ZGaeApplicationUtil.isAppYamlResource((IFile) aResource))
            {
                handleAppYaml(aResource);
            }
        }
        return true;
    }

    /**
     * Handles a single app.yaml file (which represents a Google App Engine
     * Application).  This method will create the {@link IZGaeApplication} from
     * the app.yaml file.
     * 
     * @param aResource
     */
    public void handleAppYaml(IResource aResource)
    {
        ZGaeApplicationModel model = new ZGaeApplicationModel(aResource.getParent());
        IZGaeApplication application = ZGaeApplicationFactory.getInstance().createApplication(model);
        getApplications().add(application);
    }

    /**
     * @return the applications
     */
    public Set<IZGaeApplication> getApplications()
    {
        return mApplications;
    }

    /**
     * @param aApplications the applications to set
     */
    protected void setApplications(Set<IZGaeApplication> aApplications)
    {
        mApplications = aApplications;
    }
}
